package main

import (
	"log"
	"qimo_work/config"
	"qimo_work/handlers"
	"qimo_work/middlewares"

	"github.com/gin-contrib/sessions"
	"github.com/gin-contrib/sessions/cookie"
	"github.com/gin-gonic/gin"
)

func main() {

	// 加载 .env 文件并初始化数据库连接
	config.InitDB()
	defer config.CloseDB() // 确保程序退出时关闭数据库连接

	// 创建 Gin 默认路由
	r := gin.Default()
	// session和cookie
	// 设置session中间件
	store := cookie.NewStore([]byte("secret"))
	r.Use(sessions.Sessions("mysession", store))
	//文件静态访问
	r.Static("/upload", "./upload")
	r.Static("/uploads", "./uploads")

	// 全局中间件：日志记录
	r.Use(middlewares.LoggingMiddleware())

	// 启用跨域请求支持
	r.Use(middlewares.CORSMiddleware())
	// 文件上传路由
	r.POST("/upload", handlers.UploadAttachment)                   // 上传文件路由
	r.POST("/upload-multiple", handlers.UploadMultipleAttachments) // 多附件上传
	// 删除文件路由
	r.DELETE("/delete-attachment/:filename", handlers.DeleteAttachment)
	// 用户路由
	r.POST("/register", handlers.Register)   // 注册
	r.POST("/login", handlers.Login)         // 登录
	r.POST("/logout", handlers.Logout)       // 新增：登出
	r.GET("/check-auth", handlers.CheckAuth) // 新增：检查认证状态

	r.POST("/article_release", middlewares.AuthMiddleware(), handlers.CreatePost) // 文章管理，添加身份验证
	r.GET("/posts", handlers.GetAllPosts)
	r.GET("/posts/search", handlers.SearchPostsHandler)                               //搜索文章
	r.GET("/posts/:id", handlers.GetPost)                                             // 获取单篇文章
	r.DELETE("/posts/:id", handlers.DeletePost)                                       // 删除文章
	r.PUT("/posts/:id/like", handlers.LikePost)                                       // 点赞文章
	r.PUT("/posts/:id/favorite", middlewares.AuthMiddleware(), handlers.FavoritePost) // 收藏文章
	r.GET("/favorites", middlewares.AuthMiddleware(), handlers.GetUserFavoritePosts)  // 获取收藏文章
	r.DELETE("/favorite/:post_id", handlers.RemoveFavoritePost)

	r.PUT("/posts/:id", handlers.UpdatePost)

	//用户管理
	r.GET("/users", handlers.GetAllUsersHandler) // 获取用户列表
	r.GET("/users/:id", handlers.GetUserHandler)
	r.DELETE("/users/:id", handlers.DeleteUserHandler) // 删除用户
	r.PUT("/users/:id", handlers.UpdateUserHandler)    // 更新用户
	// 评论相关路由
	r.GET("/comments/:post_id", handlers.GetAllCommentsHandler)                   // 获取文章评论（分页）
	r.GET("/comment/:id", handlers.GetCommentHandler)                             // 获取单个评论
	r.POST("/comments", middlewares.AuthMiddleware(), handlers.AddCommentHandler) // 添加评论
	r.DELETE("/comment/:id", handlers.DeleteCommentHandler)                       // 删除评论
	r.PUT("/comment/:id", handlers.UpdateCommentHandler)                          // 更新评论
	//多附件
	r.GET("/attachments/:post_id", handlers.GetAttachmentsByPostID)
	// 路由配置
	r.GET("/posts/category/:category_id", handlers.GetPostsByCategory)
	r.GET("/posts/categoryall/:category_id", handlers.GetPostsByCategoryall)

	// 需要身份验证的路由
	authorized := r.Group("/admin", middlewares.AuthMiddleware()) // 在该路由组上应用身份验证中间件
	{
		authorized.GET("/register", handlers.Register)
	}

	// 启动服务器
	err2 := r.Run(":9090")
	if err2 != nil {
		log.Fatalf("Server failed to start: %v", err2)
	}
}
